package org.classg.edu.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.edu.client.UcenterClient;
import org.classg.edu.entity.Comment;
import org.classg.edu.service.CommentService;
import org.classg.utils.JwtUtils;
import org.classg.utils.R;
import org.classg.vo.UcenterMemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-12
 */
@Api(description = "评论前台管理")
@RestController
@RequestMapping("/eduservice/commentfront")
@CrossOrigin
public class CommentFrontController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation("查询所有评论分页数据")
    @GetMapping("/queryAllCommentPage/{courseId}/{current}/{limit}")
    public R queryAllCommentPage(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId,
                                 @ApiParam(name = "current", value = "当前页", required = true) @PathVariable Long current,
                                 @ApiParam(name = "limit", value = "每页显示记录数", required = true) @PathVariable Long limit) {
        Page<Comment> page = new Page<>(current, limit);
        Map<String, Object> map = commentService.queryAllCommentPage(courseId, page);
        return R.ok().data(map);
    }

    @ApiOperation("添加评论")
    @PostMapping("/insertComment")
    public R insertComment(@RequestBody Comment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(20002).message("您未登录，请登录！");
        }
        comment.setMemberId(memberId);
        // 调用用户接口
        UcenterMemberVo user = ucenterClient.queryUserById(memberId);
        comment.setNickname(user.getNickname());
        comment.setAvatar(user.getAvatar());
        commentService.save(comment);
        return R.ok().message("添加评论成功！");
    }
}


